package org.jeecg.modules.system.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.system.entity.DeviceInfo;
import org.jeecg.modules.system.mapper.DeviceInfoMapper;
import org.jeecg.modules.system.service.IDeviceInfoService;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description: 系统数据日志实现类
 * @author: jeecg-boot
 */
@Service
@Slf4j
public class DeviceInfoServiceImpl extends ServiceImpl<DeviceInfoMapper, DeviceInfo> implements IDeviceInfoService {

    /**
     *
     * @param deviceNo
     * @param type 1:升 2:降
     * @return
     */
    @Override
    public Result control(String deviceNo, Integer type) {
        String url = "https://www.dazao.online/cloud/pic/parkingLock";
//        String url = "http://mqtttest.dazao.online/cloud/pic/parkingLock";
        Map<String, String> params = new HashMap<>();
        params.put("appId", "90c41f73aae8458391596e82fb2977e3");
//        params.put("appId", "d107828479c04efd946be909a244c8e9");
        params.put("ip", "27.0.0.1");
        params.put("liftNum", deviceNo);
        params.put("liftType", String.valueOf(type));
        params.put("timeStamp", String.valueOf(DateUtil.date().getTime()));
        params.put("nonceStr", UUID.randomUUID().toString());
        params.put("signType", "MD5");
        String s = sortAndConcatenateParameters(params);
        s += "&key=74c502bfd5044b3ebdf6c95e00c7c770";
//        s += "&key=abbe80c3ab374bab8778aa5c7efa4438";
        String encode = DigestUtil.md5Hex(s);
        params.put("sign", encode);
        String jsonString = JSONObject.toJSONString(params);
        String body = HttpRequest.post(url).body(jsonString).execute().body();
        log.info("body = " + body);
        JSONObject jsonObject = JSONObject.parseObject(body);
        String errCode = jsonObject.getString("errCode");
        String errMsg = jsonObject.getString("errMsg");
        if (StringUtils.isNotBlank(errCode) && "0".equals(errCode)) {
            return Result.OK(jsonObject);
        } else {
            return Result.error(errMsg);
        }
    }

    private static String sortAndConcatenateParameters(Map<String, String> params) {
        List<Map.Entry<String, String>> sortedParams = new ArrayList<>(params.entrySet());
        Collections.sort(sortedParams, new Comparator<Map.Entry<String, String>>() {
            @Override
            public int compare(Map.Entry<String, String> entry1, Map.Entry<String, String> entry2) {
                return entry1.getKey().compareTo(entry2.getKey());
            }
        });

        StringBuilder stringBuilder = new StringBuilder();
        for (Map.Entry<String, String> entry : sortedParams) {
            String key = entry.getKey();
            String value = entry.getValue();

            if (value != null && !value.isEmpty()) {
                if (stringBuilder.length() > 0) {
                    stringBuilder.append("&");
                }
                stringBuilder.append(key).append("=").append(value);
            }
        }

        return stringBuilder.toString();
    }
}
